using System;
using System.Collections.Generic;
using System.Text;

namespace BTV.Base
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1705:LongAcronymsShouldBePascalCased")]
    public class BTVDatabaseSearch
    {
        private BTVDatabase _database;

        public BTVDatabase Database
        {
            get { return _database; }
            set { _database = value; }
        }

        public BTVDatabaseSearch(BTVDatabase db)
        {
            Database = db;
            SearchParameters = new List<SearchParameter>();
        }

        private List<SearchParameter> _searchParameters;

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1002:DoNotExposeGenericLists")]
        public List<SearchParameter> SearchParameters
        {
            get { return _searchParameters; }
            set { _searchParameters = value; }
        }

        public void AddParameter(SearchParameter s)
        {
            if (SearchParameters.Count == 0)
            {
                s.IsFirst = true;
            }
            SearchParameters.Add(s);
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public Dictionary<BTVProgram, List<BTVSchedule>> DoSearch()
        {
            Dictionary<BTVProgram, List<BTVSchedule>> r = new Dictionary<BTVProgram,List<BTVSchedule>>();
            SearchParameter s = SearchParameters[0];
            foreach (KeyValuePair<string, BTVProgram> kvp in Database.Programs)
            {
                if (kvp.Value.Name.Contains(s.Term))
                {
                    List<BTVSchedule> scheds = Database.GetProgramSchedules(kvp.Key);
                    r.Add(kvp.Value, scheds);
                }
            }
            return r;
        }
    }

    public class SearchParameter
    {
        private bool _first;

        public bool IsFirst
        {
            get { return _first; }
            set { _first = value; }
        }

        private string _field;

        public string Field
        {
            get { return _field; }
            set { _field = value; }
        }

        private string _operator;

        public string Operator
        {
            get { return _operator; }
            set { _operator = value; }
        }

        private string _term;

        public string Term
        {
            get { return _term; }
            set { _term = value; }
        }

        private string _equality;

        public string Equality
        {
            get { return _equality; }
            set { _equality = value; }
        }
    }
}
